package com.dts.evaluate.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dts.evaluate.R;
import com.dts.evaluate.bean.IssueBulletinDataBean;
import com.dts.evaluate.bean.Worker;
import com.dts.evaluate.callback.BeanCallBack;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.controller.impl.DataImpl;
import com.dts.evaluate.controller.intf.DataInterface;
import com.dts.evaluate.ui.view.TableListView;
import com.dts.evaluate.ui.view.TitleView;
import com.dts.evaluate.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class DepartBroadActivity extends BaseActivity implements TitleView.OnBackClikListener{

    @Bind(R.id.activity_depart_broad)
    LinearLayout activityDepartBroad;
    @Bind(R.id.title_view)
    TitleView titleView;
    @Bind(R.id.depart_broad_listview)
    TableListView departBroadListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_depart_broad);
        ButterKnife.bind(this);

        //设置titleView返回监听
        titleView.setOnBackClickListener(this);

        //初始化listview 添加header
        LayoutInflater inflater = LayoutInflater.from(this);
        View headerView = inflater.inflate(R.layout.depart_broad_header_layout, null);
        departBroadListview.addHeaderView(headerView);
        departBroadListview.setScales(new float[]{1f / 10, 8f / 10});

        //设置listview适配器
        final DepartListViewAdapter adapter = new DepartListViewAdapter(this);
        departBroadListview.setAdapter(adapter);

        Worker worker = (Worker) getIntent().getSerializableExtra(Constants.WORKER);
        if(worker == null){
            return;
        }

        String dataId = worker.getGscOrganCodeId();
        LogUtil.v("dataId:"+dataId);

        DataInterface dataInterface = new DataImpl();
        dataInterface.getKbsxData(Constants.KBSX_URL,Constants.TYPE_DEPART_BROAD,dataId,new BeanCallBack<IssueBulletinDataBean>(){

            @Override
            public void onResponse(boolean isFromCache, IssueBulletinDataBean issueBulletinDataBean, Request request, @Nullable Response response) {
                LogUtil.v("issueBulletinDataBean:"+issueBulletinDataBean);
                if(issueBulletinDataBean == null){
                    return;
                }
                List<IssueBulletinDataBean.IssueBulletinBean> data = issueBulletinDataBean.getIssueBulletin();
                LogUtil.v("data:"+data);
                if(data == null || data.size() == 0){
                    return;
                }
                adapter.setData(data);
            }
        },this);
    }

    @Override
    public void onTitleViewClik(View v) {
        finish();
    }


    /**
     * listview适配器
     */
    static class DepartListViewAdapter extends BaseAdapter {

        private List<IssueBulletinDataBean.IssueBulletinBean> data = new ArrayList<>();
        private LayoutInflater inflater;
        private Context context;

        public DepartListViewAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            this.context = context;
        }

        public void setData(List<IssueBulletinDataBean.IssueBulletinBean> data) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }


        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return getItem(position).hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final IssueBulletinDataBean.IssueBulletinBean issueBulletinBean = data.get(position);

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.depart_broad_item_layout, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.numberTextview.setText(String.valueOf(position + 1));
            viewHolder.titleTextview.setText(issueBulletinBean.getBulletintitle());
            viewHolder.timeTextview.setText(issueBulletinBean.getBegintime());

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,DepartBroadDetailActivity.class);
                    intent.putExtra(Constants.ISSUE_BULLETIN_BEAN,issueBulletinBean);
                    context.startActivity(intent);
                }
            });
            return convertView;
        }

        static class ViewHolder {
            @Bind(R.id.number_textview)
            TextView numberTextview;
            @Bind(R.id.title_textview)
            TextView titleTextview;
            @Bind(R.id.time_textview)
            TextView timeTextview;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
